package com.xy.common.util;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.xy.core.model.Tdictionarytype;
import com.xy.core.model.Tdictionay;
import com.xy.core.service.TdictionarytypeService;
import com.xy.core.service.TdictionayService;
import com.xy.core.util.SpringBeanUtils;

public class DictUtils {
	// return DictUtils.getDictByType(DictUtils.QJSCUSER_GENDER);
	// 字典表--类型
	public static String TDICTIONAY_TYPE = "1";

	// 用户表--性别
	public static String TUSER_SEX = "2";
	// 会员信息--性别
	public static String TMEMBER_SEX = "2";
	// 会员信息--号牌种类
	public static String TMEMBER_HPZL = "hpzl";
	// 会员信息--号牌种类
	public static String TMEMBER_HPZL2 = "hpzl";
	// 会员信息--号牌种类
	public static String TMEMBER_HPZL3 = "hpzl";
	// 用户爆料--报料类别
	public static String TMEMBERNEWS_TYPE = "bllb";
	// 用户爆料--紧急情况
	public static String TMEMBERNEWS_LEVEL = "jjqk";
	// 租房信息--市区
	public static String TRENT_SSQ = "ssq";
	// 租房信息--所属派出所
	public static String TRENT_PCS = "pcs";
	// 租房信息--房屋类别
	public static String TRENT_FWLB = "fwlb";
	// 租房信息--隐患类别
	public static String TRENT_YHLB = "yhlb";
	// 租房信息--房屋管理类别
	public static String TRENT_FWGLLB = "fwgllb";
	// 租房信息--是否出租房屋0:否1:是
	public static String TRENT_ISCZFW = "sfczw";
	// 场所信息--省市县
	public static String TCOMPANY_SSX = "ssq";
	// 场所信息--单位类别
	public static String TCOMPANY_DWLB = "dwlb";
	// 场所信息--单位性质
	public static String TCOMPANY_DWXZ = "dwxz";
	// 场所信息--辖区派出所
	public static String TCOMPANY_XQPCS = "pcs";
	// 场所信息--单位负责人证件种类
	public static String TCOMPANY_DWFZRZJZL = "zjzl";
	// 场所信息--治安负责人证件种类
	public static String TCOMPANY_ZAFZRZJZL = "zjzl";
	// 民意警务意见采集--状态
	public static String TCOMMENT_STATUS = "4";
	// 民意警务网上投诉--状态
	public static String TCOMPLAINT_STATUS = "4";
	// 居住证网上办理--是否快递
	public static String TRESIDENCE_EXPRESSAGE = "3";

	// 从业人员--性别
	public static String TWORKER_XB = "2";
	// 从业人员--文化程度
	public static String TWORKER_WHCD = "whcd";
	// 民意警务论坛话题表--状态
	public static String TTOPIC_STATUS = "3";
	// 民意警务论坛话题表--分类
	public static String TTOPIC_TYPE = "2";
	// 视频快照--状态
	public static String TFLASH_STATUS = "3";
	// 办事指南--类型
	public static String TWORKGUIDE_TYPE = "wg";
	// 问卷题目--问卷主题
	public static String TSURVEYQUESTION_SURVERYTYPE = "6";
	// 问卷题目--题目类型
	public static String TSURVEYQUESTION_QUESTIONTYPE = "5";
	 //问卷参与者--问卷主题
    public static String TSURVEYPARTNER_SURVERYTYPE="6";
	 //用户等级
    public static String TMEMBER_ULEVEL="7";

	private static Map<Object, Map<Object, String>> dict = new HashMap<Object, Map<Object, String>>();

	public static Map<Object, String> getDictByType(String type) {
		synchronized (lock) {
			checkUpdate();
		}
		if(null == dict.get(type))
			return new HashMap<Object, String>();
		else
			return dict.get(type);
	}

	public static String getNameById(Object type, String id) {
		Map<Object, String> m = dict.get(type);
		if (m != null)
			return m.get(id);
		else
			return "";
	}

	public static void init(List<Tdictionay> l) {
		dict.clear();
		for (Tdictionay d : l) {
			String type = d.getId().getType();
			String key = d.getId().getId();
			String value = d.getName();
			Map<Object, String> m = dict.get(type);
			if (m == null) {
				m = new LinkedHashMap<Object, String>();
				dict.put(type, m);
			}
			m.put(key, value);
		}
	}

	public static void addDictType(List<Tdictionarytype> l) {
		Map<Object, String> m = dict.get(TDICTIONAY_TYPE);
		if (m == null) {
			m = new HashMap<Object, String>();
			dict.put(TDICTIONAY_TYPE, m);
		}
		for (Tdictionarytype t : l) {
			m.put(t.getType(), t.getName());
		}
	}

	private static void reload() {
		dict.clear();
		List<Tdictionay> l = ((TdictionayService) SpringBeanUtils.getBean("tdictionayService")).getAll();
		init(l);
		List<Tdictionarytype> l1 = ((TdictionarytypeService) SpringBeanUtils.getBean("tdictionarytypeService")).getAll();
		addDictType(l1);
	}

	private static void checkUpdate() {
		if (System.currentTimeMillis() - lastUpdate > updateInteval) {
			lastUpdate = System.currentTimeMillis();
			reload();
		}
	}

	private static long lastUpdate = System.currentTimeMillis();
	private static long updateInteval = 15 * 60 * 1000;// 15*60*1000
	private static Object lock = new Object();
}
